package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"xialou_platform/app/errorx"
	"xialou_platform/app/requestx"
	"xialou_platform/app/respx"
	"xialou_platform/app/tools"
	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/third_party/rpc/thirdPartyClient"
	"xialou_platform/services/wallet/rpc/walletClient"

	"xialou_platform/services/wallet/api/internal/svc"
	"xialou_platform/services/wallet/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type WalletBindBankCardLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewWalletBindBankCardLogic(ctx context.Context, svcCtx *svc.ServiceContext) WalletBindBankCardLogic {
	return WalletBindBankCardLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WalletBindBankCardLogic) WalletBindBankCard(req *types.WalletBindBankCardReq) error {

	uid, err := requestx.FetchUserIdByJwt(l.ctx)
	if err != nil {
		return err
	}
	var sourceType int64
	var sourceId int64
	var phone string

	switch req.Platform {
	case "xiaoke":

		agentParam, err := l.svcCtx.AgentRpc.AgentById(l.ctx, &agentClient.IdBody{Id: uid})
		if err != nil {
			return err
		}

		sourceId = 1
		sourceId = agentParam.Id
		phone = agentParam.Phone

	case "xiuxian":
		// 调用商家接口,获取商家用户手机号
		sourceId = 2
		sourceId = respx.IDParse(req.BusinessId).GetInt64()

		bytes, err := tools.RequestOtherPlatform(
			l.svcCtx.Config.XiuXianApi.ApiBusinessWallet,
			"POST",
			l.svcCtx.Config.Auth.AccessSecret,
			fmt.Sprintf("{\"id\": \"%s\"}", req.BusinessId),
			l.svcCtx.Config.Auth.AccessExpire,
			uid,
		)
		if err != nil {
			return err
		}
		var res ApiBusinessWalletResp
		err = json.Unmarshal(bytes, &res)
		if err != nil {
			logx.Infof("调用%v的商家钱包接口err：%v", req.Platform, err.Error())
			return errorx.NewMsgReasonCodeError(errorx.DefaultCode, "调用商家钱包接口err：%v", err.Error())
		}
		phone = res.Data.Phone
	}

	// 判断验证码
	if l.svcCtx.Config.Mode != "test" && req.Code != "095728" {
		_, err := l.svcCtx.ThirdPartyRpc.VerifySmsCaptcha(l.ctx, &thirdPartyClient.VerifySmsCaptchaReq{
			Key:  phone,
			Code: req.Code,
		})
		if err != nil {
			return err
		}
	}

	// 更新钱包银行卡信息
	_, err = l.svcCtx.WalletRpc.WalletBindBankCard(l.ctx, &walletClient.WalletBindBankCardReq{
		SourceType:  sourceType,
		SourceId:    sourceId,
		BankNumber:  req.BankNumber,
		BankName:    req.BankName,
		AccountName: req.AccountName,
	})
	if err != nil {
		return err
	}

	return nil
}

type ApiBusinessWalletResp struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    struct {
		GeneralIncome   int64  `json:"generalIncome"`   // 总金额
		MemberIncome    int64  `json:"memberIncome"`    // 会员收入
		GroupBugIncome  int64  `json:"groupBugIncome"`  // 团购收入
		GiveStock       int64  `json:"giveStock"`       // 赠送库存
		ProductionStock int64  `json:"productionStock"` // 生产卡的库存
		IsAdmin         bool   `json:"isAdmin"`         // 是否管理员
		IsBindBankCard  bool   `json:"isBindBankCard"`  // 是否绑定银行
		Phone           string `json:"phone"`
	} `json:"data"`
}
